package com.ai.module.chat.util;

import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

import java.util.Random;

/**
 * @author: MrHe
 * @create: 2024-01-31 23:27
 * @description:
 **/
@Slf4j
public class InviteCodeUtil {
    private static String BASE_CODE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    public static String getCode4(int id){

        return getStringBuilder(id, 4);

    }
    public static String getCode6(int id){

        return getStringBuilder(id, 6);

    }

    @NotNull
    private static String getStringBuilder(int id, int baseSize) {

        StringBuilder sb = new StringBuilder(String.valueOf(id));
        try {
            for (int index = 0; index < baseSize; index++) {
                Random r = new Random();
                int realIndex = r.nextInt(52);
                sb.append( BASE_CODE.charAt(realIndex));
            }
        }catch (Exception ex){
            ex.printStackTrace();
            log.error("生成邀请码异常 message[{}]",ex.getMessage());
            return "ABCDEF";
        }
        return sb.toString();
    }

    public static int getMessageCode( int baseSize) {

        StringBuilder sb = new StringBuilder();
        try {
            for (int index = 0; index < baseSize; index++) {
                Random r = new Random();
                int realIndex = r.nextInt(10);
                sb.append( "0123456789".charAt(realIndex));
            }
        }catch (Exception ex){
            ex.printStackTrace();
            log.error("生成邀请码异常 message[{}]",ex.getMessage());
            return 12345;
        }
        return Integer.parseInt(sb.toString());
    }


    public static void main(String[] args) {
        for (int i = 0; i < 10000; i++) {
            int messageCode = getMessageCode(5);
            System.out.println("messageCode = " + messageCode);
        }
        ;
    }
}
